package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.commonutils.orderVo.CourseVo;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.frontVo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontVo.CourseWebVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduChapterService eduChapterService;

    //条件查询带分页
    @PostMapping("getCourseFrontList/{page}/{limit}")
    public R getCourseFrontList(@PathVariable long page,
                                @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> p = new Page<>(page,limit);
        Map<String,Object> map = eduCourseService.getCourseFrontList(p,courseFrontVo);
        System.out.println(map);
        return R.ok().data(map);
    }

    //课程详情信息
    @GetMapping("getCourseFrontInfo/{courseId}")
    public R getCourseFrontInfo(@PathVariable String courseId){
        //编写sql语句查询课程信息
        CourseWebVo courseWebVo = eduCourseService.getBaseInfo(courseId);
        //根据课程id查询所有的章节和小节
        List<ChapterVo> chapterVos = eduChapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVos",chapterVos);
    }

    //根据课程id查询课程信息
    @GetMapping("getCourseInfoOrder/{id}")
    public CourseVo getCourseInfoOrder(@PathVariable String id){
        CourseVo courseVo = new CourseVo();
        CourseWebVo info = eduCourseService.getBaseInfo(id);
        BeanUtils.copyProperties(info,courseVo);
        return courseVo;
    }
}
